package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.Cupon;
import com.wn.class30.service.Couponservice;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 优惠券管理
 */
@RestController
@Slf4j
public class CouponContoller {
    @Resource
    private Couponservice couponservice;

    @RequestMapping("/houtai/page/coupon/list")
    @RequiresPermissions("/houtai/page/coupon/list")
    public Result selectCoupons(long page, Integer limit){
        log.info("进入了查询的方法");
        List<Cupon> cuponList = couponservice.selectCoupons(page, limit);
        Page page1 = new Page();
        long dataCount = couponservice.count();
        int pageCount = (int)(dataCount%limit==0?dataCount/limit:dataCount/limit+1);
        page1.setTotalSize(dataCount);
        page1.setPageSize(limit);
        page1.setCurrentPage(page);
        page1.setTotalPage(pageCount);
        return new Result("200","success",page1,cuponList);
    }

    @RequestMapping("/houtai/page/coupon/insert")
    @RequiresPermissions("/houtai/page/coupon/insert")
    public Result insertCoupons(Cupon cupon){
        //System.out.println("-------------------------------");
        //System.out.println(cupon);
        couponservice.insertSelective(cupon);
        return new Result("200","success",null,null);
    }

    @RequestMapping("/houtai/page/coupon/update")
    @RequiresPermissions("/houtai/page/coupon/update")
    public Result updateCoupons(Cupon cupon){
        log.info("进入了修改的方法");
        log.info(String.valueOf(cupon));
        couponservice.updateByPrimaryKeySelective(cupon);
        return new Result("200","success",null,null);
    }

    @RequestMapping("/houtai/page/coupon/delete")
    @RequiresPermissions("/houtai/page/coupon/delete")
    public Result deleteCoupons(Integer couponId){
        //log.info("进入了删除的方法");
        System.out.println(couponId);
        couponservice.deleteByPrimaryKey(couponId);
        return new Result("200","success",null,null);
    }

    /**
     * 批量删除
     * @param couponIds
     * @return
     */
    @RequestMapping("/houtai/page/coupon/deletes")
    @RequiresPermissions("/houtai/page/coupon/delete")
    public Result deleteCoupons(@RequestParam("couponIds[]") int[] couponIds){
        for (int i = 0; i <couponIds.length ; i++) {
            int couponId= couponIds[i];
            //System.out.println(couponId);
            couponservice.deleteByPrimaryKey(couponId);
        }
        return new Result("200","success",null,null);
    }
}
